package service

import (
	"mutu/misc"
	"mutu/misc/log"
	"mutu/models"
)

type UserManager struct {
}

var (
	UserService = UserManager{}
)

func (p UserManager) CreateUser(user *models.User) error {
	if user.Uuid == "" {
		user.Uuid = misc.NewUUID()
	}
	user.Enable = true
	user.Deleted = false
	if _, err := models.Orm.Insert(user); err != nil {
		return err
	}

	BinderService.CreateDefaultUserBinder(user.Uuid)
	//TODO RECORD error

	return nil
}

func (p UserManager) GetUserByUuid(uuid string) (*models.User, error) {
	m := &models.User{
		Uuid: uuid,
	}
	err := models.Orm.Read(m, "Uuid")
	if err != nil {
		log.Log.Error("读取用户<%s>出错，%s", uuid, err)
		return nil, err
	}

	return m, nil
}
